/*
 * @Author: zhangxuanjiu
 * @Date: 2017-08-10 14:38:28
 * @Last Modified by: zhangxuanjiu
 * @Last Modified time: 2017-08-10 15:08:46
 */

const path = require('path');
const webpack = require('webpack');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const serverConfig = require('./serverConfig.js')

module.exports = {
  resolve: {
    modules: [
      path.join(__dirname, 'src'),
      'node_modules'
    ],
    extensions: ['.js', '.jsx', '.css', '.less', '.vue'],
    alias: {
      'vue$': 'vue/dist/vue.js'
    }
  },
  context: path.join(__dirname, 'src'),
  entry: {
    app: [
      './index.js',
      `webpack-dev-server/client?http://${serverConfig.host}:${serverConfig.port}/`,
      'webpack/hot/dev-server',
      // 'webpack/hot/only-dev-server'
    ],
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js',
    publicPath: './',
    chunkFilename: '[name].js?[hash:8]'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"development"'
      }
    }),
    new OpenBrowserPlugin({ url: `http://${serverConfig.host}:${serverConfig.port}` })
  ],
  module: {
    rules: [
      // {
      //   test: /\.(js|vue)$/,
      //   loader: 'eslint-loader',
      //   enforce: 'pre',
      //   options: {
      //     formatter: require('eslint-friendly-formatter')
      //   }
      // },
      {
        test: /\.vue$/,
        use: ['vue-loader']
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.css/,
        use: ['style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.less/,
        use: ['style-loader', 'css-loader', 'autoprefixer-loader', 'less-loader']
      },
      {
        test: /\.(png|jpg|gif|svg|eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]'
        }
      }
    ],

  }
};